from typing import Any
import numpy as np

class logit_regrassion:
    def __init__(self, alpha=1) -> None:
        self.w = np.array([0, 0, 1]) # 两个特征w加一个b
        self.alpha = alpha
        

    def forward(self, x):
        x = np.append(x, 1)     # 添加一个1以计算b
        y = 1 / (1 + np.power(np.e, -(self.w.T @ x)))
        # print(self.w)
        if y >= 0.5:
            return 1
        else:
            return 0
    
    def backward(self, x: np.ndarray, label):
        x = np.append(x, 1)     # 添加一个1以计算b
        # print(self.w)
        if label == 1:
            self.w = (self.w.T + self.alpha * x * (np.power(np.e, (self.w.T @ x)) / np.power(np.power(np.e, (self.w.T @ x)) + 1, 2)))
        else:
            self.w = (self.w.T - self.alpha * x * (np.power(np.e, (self.w.T @ x)) / np.power(np.power(np.e, (self.w.T @ x)) + 1, 2)))

    def get_w(self):
        return self.w